package com.ibm.factories;


import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.datamodel.timeseries.SchemaFormat;
import com.ibm.datamodel.timeseries.parser.JsonParser;
import com.ibm.datamodel.timeseries.parser.ProtobufParser;
import com.ibm.datamodel.timeseries.parser.TLVParser;
import com.ibm.datamodel.timeseries.parser.XmlParser;
import com.ibm.interfaces.IMessageParser;
import com.ibm.interfaces.ITimeSeriesSchema;
import com.ibm.util.ErrorCode;
import com.ibm.util.exception.CPSException;

import ring.middleware.session.memory__init;

public class MessageParserFactory {
	private static IMessageParser createMessageParser;

	/**
	 * get schema instance according to metadata 
	 * @param tenantid
	 * @param schemaId
	 * @param metadata
	 * @return
	 * @throws CPSException
	 */
	public static IMessageParser createMessageParser(SchemaFormat schemaFormat, ITimeSeriesSchema schema) throws CPSException {
		return createMessageParser(schemaFormat, schema, null);
	}
	
	static public IMessageParser createMessageParser(SchemaFormat format, ITimeSeriesSchema schema, ObjectNode metadata) throws CPSException {

		switch(format){
		case json:
			return new JsonParser(schema);
		case protobuf:
			return new ProtobufParser(schema);
		case xml:
			return new XmlParser(schema);
		case tlv:
			return new TLVParser(schema, metadata);
		default:
			throw new CPSException(ErrorCode.ERROR_MESSAGE_TYPE, "do not support the message type: " + format.name());
		}
	}

	public static IMessageParser createMessageParser(String schemaFormat, ITimeSeriesSchema schema) throws CPSException {
		return createMessageParser(schemaFormat, schema, null);
	}
	
	public static IMessageParser createMessageParser(String schemaFormat, ITimeSeriesSchema schema, ObjectNode metadata) throws CPSException {
		switch(schemaFormat){
		case "json":
			return new JsonParser(schema);
		case "protobuf":
			return new ProtobufParser(schema);
		case "xml":
			return new XmlParser(schema);
		case "tlv":
			return new TLVParser(schema, metadata);
		default:
			throw new CPSException(ErrorCode.ERROR_MESSAGE_TYPE, "do not support the message type: " + schemaFormat);
		}
	}

}
